#include "PopupStatisticsUpdateMsg.hpp"

using namespace std;
using namespace Popup;


StatisticsUpdateMsg::StatisticsUpdateMsg(const Message *p_message)
: AbstractMessage(POPUP_MSG_TYPE_STATISTICS_UPDATE)
{
  AbstractMessage::deserialize(p_message);
}

bool StatisticsUpdateMsg::onSend()
{
  bool _rc = true;

  if (stats.size() == 0) {
    _rc = false;
  }
  else {
    unsigned short _item = 0;

    _rc = payloadAddLong(USERID, userID);

    payloadAddShort(RECORDMASK, stats.recordMask);

    map<UserRateItem, unsigned int>::const_iterator _it;
    for (_it = stats.begin(); _it != stats.end(); _it++) {
      _item = (unsigned short) _it->first;
      payloadAddLong(_item, _it->second);
    }
  }

  return _rc;
}

bool StatisticsUpdateMsg::onReceive()
{
  unsigned int _value = 0;

  bool _rc = payloadGetLong(USERID, userID);

  if (_rc) {
    payloadGetShort(RECORDMASK, stats.recordMask);

    if (payloadGetLong(POPUP_RATE_LIKE, _value)) {
      stats[POPUP_RATE_LIKE] = _value;
    }
    if (payloadGetLong(POPUP_RATE_DONTLIKE, _value)) {
      stats[POPUP_RATE_DONTLIKE] = _value;
    }
    if (payloadGetLong(POPUP_RATE_POETRY, _value)) {
      stats[POPUP_RATE_POETRY] = _value;
    }
    if (payloadGetLong(POPUP_RATE_FUCKYOU, _value)) {
      stats[POPUP_RATE_FUCKYOU] = _value;
    }
  }
  return _rc;
}
